package com.example.mercury.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;

import com.example.mercury.MainActivity;
import com.example.mercury.R;
import com.example.mercury.entity.Register;
import com.example.mercury.entity.User;
import com.example.mercury.utils.HttpUtils;
import com.example.mercury.utils.JsonUtils;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author 盛守义
 * @createTime 2021/10/24 15:43
 * @projectName TurningCoffee
 * @className RegistActivity.java
 * @description TODO
 */
public class RegistActivity extends Activity {
    private EditText et_mail, et_pwd, et_verify, et_username;
    private AppCompatButton btn_submit;
    private Intent intent;
    private String email, pwd, verify, username;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();

        setListener();
    }

    void initView(){
        et_mail = findViewById(R.id.et_emailrgt);
        et_pwd = findViewById(R.id.et_pwdrgt);
        et_verify = findViewById(R.id.et_verifyrgt);
        et_username = findViewById(R.id.et_username);
        btn_submit = findViewById(R.id.btn_submitrgt);
    }

    void initValue(){
        email = et_mail.getText().toString().trim();
        pwd = et_pwd.getText().toString().trim();
        verify = et_verify.getText().toString().trim();
        username = et_username.getText().toString().trim();
    }

    void setListener(){
        subClickListener listener = new subClickListener();
        btn_submit.setOnClickListener(listener);
    }

    class subClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            initValue();
            if (email == null || email.equals("") || pwd == null || pwd.equals("") || verify == null || verify.equals("")||username == null || username.equals("")) {
                Toast.makeText(getApplicationContext(), "输入不能为空", Toast.LENGTH_SHORT).show();
            } else {
                Gson gson = new Gson();

                User user = new User();
                user.setUserPassword("123789456");
                user.setUserTel("75894462134@qq.com");
                user.setUserNickname("不能为空");

                String jsonString = gson.toJson(user);
                Log.e("jsonString:", jsonString);
                //发送用户名密码并接收
                HttpUtils httpUtils = new HttpUtils();
                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), jsonString);

                //输入url
                Call call = httpUtils.getCall(requestBody, "/user/center-user/register");
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        RegistActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        });

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String res = response.body().string();
                        Log.e("返回信息", res);
                        Register register = new JsonUtils().getRegister(res);
                        if (register.isSuccess() == true) {
                            finish();
                        }
                    }
                });
            }
        }
    }
}
